#include "dealCommand.h"

//命令判断V1.0

int dealCommand(const char*Command, const char*Path, int netfd)
{
    char *command = strdup(Command);
    char*path = strdup(Path);

    char firstWord[1024] = {0};
    char fileName[1024] = {0};

    char *file_word = strtok(command, " ");
    strcpy(firstWord, file_word);
    char *file_name = strtok(NULL, "");
    if(file_name != NULL) {
        strcpy(fileName, file_name);   
    }

    char server_path[1024];
    getcwd(server_path,sizeof(server_path));
    strcat(server_path, "/");
    strcat(server_path, path);

    int ret = 1;
    int ifret = 0;
    if(strcmp(firstWord,"cd") == 0) {  // cd 发送 2  冒号前地址  ret 
        // 如果cd后没有参数
        if(strlen(fileName) == 0){
            printf("Invalid command: cd!\n");
            return 0;
        }
        ret = cdDir(netfd, server_path, fileName);
        ifret = 1;
    }
    else if(strcmp(firstWord,"ls") == 0) {  // ls 发送 4  冒号前地址 打印二维数组 数组个数 ret 
        ret = lsDir(netfd, server_path);
        ifret = 1;
    }
    else if(strcmp(firstWord,"puts") == 0) {
        // 如果puts后没有参数
        if(strlen(fileName) == 0){
            printf("Invalid command: puts!\n");
            return 0;
        }
        puts_server(netfd,server_path);
        ret = 1;
        ifret = 0;
    }
    else if(strcmp(firstWord,"gets") == 0){ 
        // 如果gets后没有参数
        if(strlen(fileName) == 0){
            printf("Invalid command: gets!\n");
            return 0;
        }
        gets_server(netfd,server_path);
        ret = 1;
        ifret = 0;
    }
    else if(strcmp(firstWord,"remove") == 0) {  // remove 发送 2  冒号前地址  ret 
        // 如果remove后没有参数
        if(strlen(fileName) == 0){
            printf("Invalid command: remove!\n");
            return 0;
        }
        ret = rmFile(netfd, server_path, fileName);
        ifret = 1;
    }
    else if(strcmp(firstWord,"pwd") == 0) {  // pwd 发送 2  冒号前地址  ret 
        ret = pwdDir(netfd, server_path);
        ifret = 1;
    }
    else if(strcmp(firstWord,"mkdir") == 0) {  // mkdir 发送 2  冒号前地址  ret 
        // 如果mkdir后没有参数
        if(strlen(fileName) == 0){
            printf("Invalid command: mkdir!\n");
            return 0;
        }
        ret = mkDir(netfd, server_path, fileName);
        ifret = 1;
    }
    else if(strcmp(firstWord,"rmdir") == 0) {  // rmdir 发送 2  冒号前地址  ret 
        // 如果rmdir后没有参数
        if(strlen(fileName) == 0){
            printf("Invalid command: rmdir!\n");
            return 0;
        }
        ret = rmDir(netfd, server_path, fileName);
        ifret = 1;
    }
    else{
        printf("命令不合法：%s\n\n", firstWord);
        return 0;
    }

    char rett[1024] = {0};
    if(ret == -1) {
        strcpy(rett, "false");
    }
    else if(ret == 1) {
        strcpy(rett, "true");
    }
    //printf("rett = %s\n",rett);

    train_t train;
    if(ifret == 1) {
        train.length = strlen(rett);
        memcpy(train.data, rett, train.length);
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        //printf("train.data = %s\n", train.data);
    }

    free(command);
    free(path);
    return 0;
}

